package com.meetingshow.meeting.actions;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.meeting.uiservice.IUIMeetingService;
import com.meetingshow.meeting.uiservice.model.CityVO;
import com.meetingshow.meeting.uiservice.model.IndustryVO;
import com.meetingshow.meeting.uiservice.model.MeetingQueryParasVO;
import com.meetingshow.meeting.uiservice.model.MeetingTypeVO;
import com.meetingshow.meeting.uiservice.model.MeetingVO;
import com.meetingshow.utils.UIServiceFactory;

@Namespace("/metadmin")
public class MeetingAdminAction extends MeetingBaseAction {
    /**
     * 
     */
    private static final long serialVersionUID = -2905173474492480968L;
    
    private MeetingVO meetingVO = new MeetingVO();
    private MeetingQueryParasVO meetingQueryParasVO = new MeetingQueryParasVO();
    private String message;
    private List<MeetingVO> meetingList = new ArrayList<MeetingVO>();
    private Map<Integer, String> cities;
    private Map<Integer, String> industries;
    private Map<Integer, String> meetingTypes;
    
    private final static Logger logger = Logger.getLogger(MeetingAdminAction.class);
    
    @Action(value = "toManage", 
            results={
                @Result(name="success", location="/jsp/meeting/manageMeeting.jsp")})
    public String toManageMeeting() {
        return SUCCESS;
    }
    
    @Action(value = "toSave", 
            results={
                @Result(name="success", location="/jsp/meeting/saveMeeting.jsp"),
                @Result(name="input", location="/jsp/meeting/saveMeeting.jsp")})
    public String toSaveMeeting() {
        loadData();
        if (StringUtils.isNotBlank(message)) {
            return INPUT;
        }
        return SUCCESS;
    }
    
    @Action(value = "save", 
            results={
                @Result(name="success", location="/jsp/meeting/saveMeeting.jsp"),
                @Result(name="input", location="/jsp/meeting/saveMeeting.jsp")})
    public String saveMeeting() {
        loadData();
        try {
            IUIMeetingService meetingService = UIServiceFactory.getUIMeetingService();
            meetingService.saveMeeting(meetingVO);
            message = "添加成功";
        } catch (BaseUIServiceException e) {
            logger.error(e.getMessage(), e);
            message = "添加失败";
            return INPUT;
        }
        
        return SUCCESS;
    }
    
    @Action(value = "toUpdate",
            results={
            @Result(name="success", location="/jsp/meeting/updateMeeting.jsp"),
            @Result(name="input", location="/jsp/meeting/updateMeeting.jsp")})
    public String toUpdateMeeting() {
        loadData();
        if (StringUtils.isNotBlank(message)) {
            return INPUT;
        }
        
        if (meetingVO.getMeetingId() == null) {
            return INPUT;
        }
        
        IUIMeetingService meetingService = UIServiceFactory.getUIMeetingService();
        try {
            meetingVO = meetingService.loadMeetingById(meetingVO.getMeetingId());
        } catch (BaseUIServiceException e) {
            logger.error(e.getMessage(), e);
            message = "加载会议失败";
            return INPUT;
        }
        
        if (meetingVO == null) {
            message = "加载会议失败";
            return INPUT;
        }
        
        return SUCCESS;
    }
    
    @Action(value = "update",
            results={
            @Result(name="success", location="/jsp/meeting/updateMeeting.jsp"),
            @Result(name="input", location="/jsp/meeting/updateMeeting.jsp")})
    public String updateMeeting() {
        loadData();
        if (StringUtils.isNotBlank(message)) {
            return INPUT;
        }
        IUIMeetingService meetingService = UIServiceFactory.getUIMeetingService();
        try {
            meetingService.updateMeeting(meetingVO);
            message = "更新成功";
        } catch (BaseUIServiceException e) {
            logger.error(e.getMessage(), e);
            message = "更新失败";
            return INPUT;
        }
        return SUCCESS;
    }
    
    @Action(value = "toQuery",
            results = {
            @Result(name="success", location="/jsp/meeting/queryMeeting.jsp"),
            @Result(name="input", location="/jsp/meeting/queryMeeting.jsp")})
    public String toQueryMeeting() {
        loadData();
        if (StringUtils.isNotBlank(message)) {
            return INPUT;
        }
        return SUCCESS;
    }

    @Action(value = "query",
            results = {
            @Result(name="success", location="/jsp/meeting/queryMeeting.jsp"),
            @Result(name="input", location="/jsp/meeting/queryMeeting.jsp")})
    public String queryMeeting() {
        loadData();
        if (StringUtils.isNotBlank(message)) {
            return INPUT;
        }
        
        IUIMeetingService uiMeetingService = UIServiceFactory.getUIMeetingService();
        try {
            QueryResults<MeetingVO> queryResults = uiMeetingService.queryMeeting(meetingQueryParasVO);
            if (QueryResults.resultsListNotNull(queryResults)) {
                meetingList = queryResults.getResultList();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            message = "查询失败";
            return INPUT;
        }
        
        return SUCCESS;
    }
    
    @Action(value = "remove")
    public String remove() {
        IUIMeetingService uiMeetingService = UIServiceFactory.getUIMeetingService();
        try {
            uiMeetingService.removeMeeting(meetingVO);
        } catch (BaseUIServiceException e) {
            logger.error(e.getMessage(), e);
        }
        
        return null;
    }
    
    private void loadData() {
        try {
            List<CityVO> cityList = loadCities();
            cities = new HashMap<Integer, String>();
            if (cityList != null) {
                for (CityVO city : cityList) {
                    cities.put(city.getCityId(), city.getName());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            message = "加载城市失败";
        }
        
        try {
            List<IndustryVO> industryList = loadIndustries();
            industries = new HashMap<Integer, String>();
            if (industryList != null) {
                for (IndustryVO industry : industryList) {
                    industries.put(industry.getIndustryId(), industry.getName());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            message = "加载行业失败";
        }
        
        try {
            List<MeetingTypeVO> meetingTypeList = loadMeetingTypes();
            meetingTypes = new HashMap<Integer, String>();
            if (meetingTypeList != null) {
                for (MeetingTypeVO meetingType : meetingTypeList) {
                    meetingTypes.put(meetingType.getTypeId(), meetingType.getTypeName());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            message = "加载会议类型失败";
        }
    }

    public MeetingVO getMeetingVO() {
        return meetingVO;
    }

    public void setMeetingVO(MeetingVO meetingVO) {
        this.meetingVO = meetingVO;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCities(Map<Integer, String> cities) {
        this.cities = cities;
    }

    public void setIndustries(Map<Integer, String> industries) {
        this.industries = industries;
    }

    public void setMeetingTypes(Map<Integer, String> meetingTypes) {
        this.meetingTypes = meetingTypes;
    }

    public Map<Integer, String> getCities() {
        return cities;
    }

    public Map<Integer, String> getIndustries() {
        return industries;
    }

    public Map<Integer, String> getMeetingTypes() {
        return meetingTypes;
    }

    public MeetingQueryParasVO getMeetingQueryParasVO() {
        return meetingQueryParasVO;
    }

    public void setMeetingQueryParasVO(MeetingQueryParasVO meetingQueryParasVO) {
        this.meetingQueryParasVO = meetingQueryParasVO;
    }

    public List<MeetingVO> getMeetingList() {
        return meetingList;
    }

    public void setMeetingList(List<MeetingVO> meetingList) {
        this.meetingList = meetingList;
    }

    public String getStartDate() {
        if (meetingVO != null
                && meetingVO.getStartDate() != null)
            return DateFormatUtils.format(meetingVO.getStartDate(), "yyyy-MM-dd");
        else
            return "";
    }

    public void setStartDate(String startDate) {
        if (StringUtils.isNotBlank(startDate)) {
            try {
                meetingVO.setStartDate(DateUtils.parseDate(startDate, new String[]{"yyyy-MM-dd"}));
            } catch (ParseException e) {
                logger.error(e.getMessage(), e);
                meetingVO.setStartDate(null);
            }
        }
    }

    public String getEndDate() {
        if (meetingVO != null
                && meetingVO.getEndDate() != null)
            return DateFormatUtils.format(meetingVO.getEndDate(), "yyyy-MM-dd");
        else
            return "";
    }

    public void setEndDate(String endDate) {
        if (StringUtils.isNotBlank(endDate)) {
            try {
                meetingVO.setEndDate(DateUtils.parseDate(endDate, new String[]{"yyyy-MM-dd"}));
            } catch (ParseException e) {
                logger.error(e.getMessage(), e);
                meetingVO.setEndDate(null);
            }
        }
    }
}
